package bingosoft.metro.datasync.api;

import bingosoft.components.base.api.BaseApiController;
import bingosoft.components.base.service.PageQueryRequest;
import bingosoft.components.base.service.Result;
import bingosoft.components.base.service.ResultEntity;
import bingosoft.metro.datasync.service.DatasyncService;
import leap.core.annotation.Inject;
import leap.core.annotation.M;
import leap.core.doc.annotation.Doc;
import leap.web.annotation.Path;
import leap.web.annotation.http.GET;
import leap.web.api.mvc.ApiResponse;
import leap.web.json.JsonSerialize;

import java.util.Map;

/**
 * @author chenwx
 * @description 数据同步服务控制类
 * @createTime 2019-11-25 11:05
 */
@Path("/datasync")
@JsonSerialize(dateFormat = "yyyy-MM-dd HH:mm:ss")
public class DatasyncController extends BaseApiController {

    @Inject @M
    DatasyncService dataSyncService;

    public DatasyncController(){
        this.setLogClass(this.getClass());
        this.logMethodCalled("DataSyncController-构造函数");
    }

    /**
     * 执行组织全量同步
     * @return 同步结果
     */
    @GET("execOrgFullSync")
    @Doc("执行组织全量同步")
    public ApiResponse execOrgFullSync(){
        this.logMethodCalled("execOrgFullSync");

        Result result = dataSyncService.execOrgFullSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 执行组织增量同步
     * @return 同步结果
     */
    @Doc("执行组织增量同步")
    @GET("execOrgIncSync")
    public ApiResponse execOrgIncSync(){
        this.logMethodCalled("execOrgIncSync");

        Result result = dataSyncService.execOrgIncSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 执行用户全量同步
     * @return 同步结果
     */
    @Doc("执行用户全量同步")
    @GET("execUserFullSync")
    public ApiResponse execUserFullSync(){
        this.logMethodCalled("execUserFullSync");

        Result result = dataSyncService.execUserFullSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 执行用户增量同步
     * @return 同步结果
     */
    @Doc("执行用户增量同步")
    @GET("execUserIncSync")
    public ApiResponse execUserIncSync(){
        this.logMethodCalled("execUserIncSync");

        Result result = dataSyncService.execUserIncSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 分页查询同步日志
     * @return 分页查询结果
     */
    @Doc("分页查询同步日志")
    @GET("pageQuerySyncLog")
    public ApiResponse pageQuerySyncLog(@Doc("分页查询参数") PageQueryRequest pageQueryRequest, @Doc("其他参数") Map<String, Object> params){
        this.logMethodCalled("pageQuerySyncLog");
        this.logParamValue("pageQueryRequest", pageQueryRequest);
        this.logParamValue("params", params);

        ResultEntity resultEntity = dataSyncService.pageQuerySyncLog(pageQueryRequest, params);
        if(resultEntity.isSuccess()){
            return ApiResponse.ok(resultEntity.getBusinessObject());
        }else{
            return ApiResponse.badRequest(resultEntity.getMessage());
        }
    }
}
